home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / hist.c < prev    next >
C/C++ Source or Header  |  1993-08-22  |  14KB  |  543 lines

  1.  
  2.    /**************************************************
  3.    *
  4.    *   file d:\cips\hist.c
  5.    *
  6.    *   Functions: This file contains
  7.    *       calculate_histogram
  8.    *       calculate_histogram
  9.    *       zero_histogram
  10.    *       perform_histogram_equalization
  11.    *       show_histogram
  12.    *       print_histogram
  13.    *       smooth_histogram
  14.    *       display_histogram
  15.    *
  16.    *   Purpose: These functions calculate and display 
  17.    *      the histogram of an input image array.
  18.    *
  19.    *   Modifications:
  20.    *       July 86 - ported to IBM-PC
  21.    *       August 1990 - modified for use in the
  22.    *           C Image Processing System
  23.    *       March 1992 - removed the hardwired values
  24.    *           of 100 and replaced them with ROWS
  25.    *           and COLS.  There are still some
  26.    *           hardwired numbers in this file, but
  27.    *           they deal with displaying a histogram.
  28.    *       October 4, 1992 - added the smooth histogram
  29.    *           function.
  30.    *
  31.    **************************************************/
  32.  
  33. #include "cips.h"
  34.  
  35.  
  36. #define PRINT_WIDTH  80
  37. #define FORMFEED     '\014'
  38.  
  39.  
  40.  
  41.  
  42.           /*****************************************
  43.           *
  44.           *    perform_histogram_equalization(...
  45.           *
  46.           *    This function performs histogram
  47.           *    equalization on the input image array.
  48.           *
  49.           ******************************************/
  50.  
  51. perform_histogram_equalization(image, histogram,
  52.                                new_grays, area)
  53.    float new_grays, area;
  54.    short image[ROWS][COLS];
  55.    unsigned long histogram[];
  56. {
  57.    int i,
  58.        j,
  59.        k;
  60.    unsigned long sum,
  61.             sum_of_h[256];
  62.  
  63.    double constant;
  64.  
  65.    sum = 0;
  66.    for(i=0; i<256; i++){
  67.       sum         = sum + histogram[i];
  68.       sum_of_h[i] = sum;
  69.    }
  70.  
  71.       /* constant = new # of gray levels div by area */
  72.    constant = new_grays/area;
  73.    for(i=0; i<ROWS; i++){
  74.       for(j=0; j<COLS; j++){
  75.          k           = image[i][j];
  76.          image[i][j] = sum_of_h[k] * constant;
  77.       }
  78.    }
  79. }  /* ends perform_histogram_equalization */
  80.  
  81.  
  82.  
  83.  
  84.         /*****************************************
  85.         *
  86.         *   zero_histogram(...
  87.         *
  88.         *   This function clears or zeros a
  89.         *   histogram array.
  90.         *
  91.         ******************************************/
  92.  
  93. zero_histogram(histogram)
  94.    unsigned long histogram[];
  95. {
  96.    int i;
  97.    for(i=0; i<=GRAY_LEVELS; i++)
  98.       histogram[i] = 0;
  99. }  /* ends zero_histogram */
  100.  
  101.  
  102.  
  103.  
  104.         /*****************************************
  105.         *
  106.         *   calculate_histogram(...
  107.         *
  108.         *   This function calculates the histogram
  109.         *   for an input image arry.
  110.         *
  111.         ******************************************/
  112.  
  113. calculate_histogram(image, histogram)
  114.    short  image[ROWS][COLS];
  115.    unsigned long histogram[];
  116. {
  117.    int i,j,k;
  118.    for(i=0; i<ROWS; i++){
  119.       for(j=0; j<COLS; j++){
  120.          k = image[i][j];
  121.          histogram[k] = histogram[k] + 1;
  122.       }
  123.    }
  124. }  /* ends calculate_histogram */
  125.  
  126.  
  127.  
  128.  
  129.         /******************************************
  130.         *
  131.         *   show_histogram(histogram)
  132.         *
  133.         *   This function shows the histogram
  134.         *   on the screen as numbers and stars.
  135.         *
  136.         *******************************************/
  137.  
  138. show_histogram(histogram)
  139.         unsigned long histogram[];
  140. {
  141.         int     count,
  142.                 i,
  143.                 j;
  144.         unsigned long max, scale;
  145.  
  146.  
  147.         max   = 0;
  148.         count = 0;
  149.  
  150.         for(i=0; i<GRAY_LEVELS; i++)
  151.            if(histogram[i] > max)
  152.               max = histogram[i];
  153.  
  154.         if(max > (70 - 12))
  155.            scale = max/(70 - 12);
  156.         else
  157.            scale = 1;
  158.  
  159.         printf("\n max=%ld scale=%ld",max, scale);
  160.  
  161.         printf("\n\ngray    count");
  162.         printf("\nlevel");
  163.  
  164.         for(i=0; i<256; i++){
  165.            if(histogram[i] == 0)
  166.               ++count;
  167.            else
  168.               count = 0;
  169.  
  170.            if(count < 2){
  171.               printf("\n %4d: %7ld",i,histogram[i]);
  172.               for(j=0; j<((int)(histogram[i]/scale));
  173.                   j++){
  174.                  printf("*");
  175.               }   /* ends loop over j             */
  176.            }      /* ends if count < 5            */
  177.         }         /* ends loop over i GRAY_LEVELS */
  178. }       /* ends show_histogram */
  179.  
  180.  
  181.  
  182.         /*********************************************
  183.         *
  184.         *   print_histogram(histogram)
  185.         *
  186.         *   This function prints the histogram
  187.         *   input to the function.
  188.         *
  189.         **********************************************/
  190.  
  191. print_histogram(histogram, name)
  192.         char name[];
  193.         unsigned long histogram[];
  194. {
  195.         char    string[300],
  196.                 output[300];
  197.  
  198.         int     count,
  199.                 i,
  200.                 j,
  201.                 line_counter,
  202.                 print_counter;
  203.         unsigned long scale, max;
  204.  
  205.         FILE *printer;
  206.  
  207.         if( (printer = fopen("prn", "w")) == NULL)
  208.            printf("\nPH> Could not open printer");
  209.         else
  210.            printf("\nPH> The print file is opened");
  211.  
  212.         max           = 0;
  213.         count         = 0;
  214.         print_counter = 0;
  215.  
  216.         for(i=0; i<256; i++)
  217.            if(histogram[i] > max)
  218.               max = histogram[i];
  219.  
  220.         if(max > (PRINT_WIDTH - 12))
  221.            scale = max/(PRINT_WIDTH - 12);
  222.         else
  223.            scale = 1;
  224.  
  225.         printf("\n max=%ld scale=%ld",max, scale);
  226.  
  227.         printf("\nPI> Print header");
  228.         line_counter = 0;
  229.         hist_long_clear_buffer(string);
  230.         sprintf(string, 
  231.            "          This image is -- %s --\n", 
  232.            name);
  233.         fputs(string, printer);
  234.         ++line_counter;
  235.  
  236.         hist_long_clear_buffer(string);
  237.         sprintf(string, " \n");
  238.         fputs(string, printer);
  239.         ++line_counter;
  240.  
  241.         hist_long_clear_buffer(string);
  242.         sprintf(string, "          gray    count\n");
  243.         fputs(string, printer);
  244.         ++line_counter;
  245.         hist_long_clear_buffer(string);
  246.         sprintf(string, "          level\n");
  247.         fputs(string, printer);
  248.         ++line_counter;
  249.  
  250.         for(i=0; i<256; i++){
  251.            if(histogram[i] == 0)
  252.               ++count;
  253.            else
  254.               count = 0;
  255.  
  256.            if(count < 2){
  257.               printf(" %4d: %7ld",i,histogram[i]);
  258.               print_counter++;
  259.               if(print_counter >= 6){
  260.                  printf("\n");
  261.                  print_counter = 0;
  262.               }  /* ends if print_counter >= 6 */
  263.  
  264.               hist_long_clear_buffer(string);
  265.               sprintf(string,
  266.                 "           %3d: %7ld ->",
  267.                 i,histogram[i]);
  268.               fputs(string, printer);
  269.               hist_long_clear_buffer(string);
  270.               sprintf(output, " ");
  271.               for(j=0; j<((int)(histogram[i]/scale)); 
  272.                   j++){
  273.                  sprintf(string, "*");
  274.                  strcat(output, string);
  275.               }         /* ends loop over j */
  276.               fputs(string, printer); 
  277.               fputc('\n', printer);
  278.               ++line_counter;
  279.               if(line_counter >= 55){
  280.                  line_counter = 0;
  281.                  putc(FORMFEED, printer);
  282.               }  /* ends if line_counter >=55  */
  283.            }  /* ends if count < 2 */
  284.         }  /* ends loop over i */
  285.         putc(FORMFEED, printer);
  286.         fclose(printer);
  287.  
  288. }        /* ends print_histogram */
  289.  
  290.  
  291.  
  292.  
  293.         /*******************************************
  294.         *
  295.         *   display_histogram(histogram)
  296.         *
  297.         *   This function shows the histogram
  298.         *   input to the function.
  299.         *
  300.         ********************************************/
  301.  
  302. display_histogram(histogram, x, y,
  303.                   line_color, data_color)
  304.         int data_color, line_color, x, y;
  305.         unsigned long histogram[];
  306. {
  307.         int     count,
  308.                 i,
  309.